package com.gitee.wsl.prop

interface AttributesContainer{

    val attributes: Attributes

    /**
     * Gets a value of the attribute for the specified [key], or return `null` if an attribute doesn't exist
     */
    fun <T : Any> AttributeKey<T>.getValueOrNull(): T? = attributes.getOrNull(this)

    fun AttributeKey<*>.isExits(): Boolean = attributes.contains(this)

    fun <T : Any> AttributeKey<T>.setValue(value: T) = attributes.put(this,value)

    fun <T : Any> AttributeKey<T>.computeIfAbsent(block: () -> T): T = attributes.computeIfAbsent(this,block)

    /**
     * Removes an attribute with the specified [key]
     */
    fun <T : Any> AttributeKey<T>.removeValue() = attributes.remove(this)

    /**
     * Removes an attribute with the specified [key] and returns its current value, throws an exception if an attribute doesn't exist
     */
    fun <T : Any> AttributeKey<T>.take(): T = attributes.take(this)

    /**
     * Removes an attribute with the specified [key] and returns its current value, returns `null` if an attribute doesn't exist
     */
    fun <T : Any> AttributeKey<T>.takeOrNull(): T? = attributes.takeOrNull(this)

}